import { observable, action } from 'mobx';
import { WeaTools, WeaLocaleProvider } from 'ecCom';
import { Modal } from 'antd';
import { hashHistory } from 'react-router';
import isEqual from 'lodash/isEqual';
import { setE9ThemeStyle, setFontSize } from '../util/themeUtil';
import * as LOGIN_API from '../apis/loginApi';

const getLabel = WeaLocaleProvider.getLabel;

export default class NavStore {
  @observable account = {};// 当前登录账号信息
  @observable accountVisible = false;// 个人设置是否可见
  @observable fontSettingVisible = false; // 字体设置是否可见
  @observable themeInfo = {
    themeFontSize: '12',
  };

  @observable menus = []; // 菜单

  constructor() {
    this.onAccountVisibleChange = this.onAccountVisibleChange.bind(this);
    this.onFontSettingVisibleChange = this.onFontSettingVisibleChange.bind(this);
  }

  @action
  getThemeInfo() {
    this.loading = true;

    const themeInfo = WeaTools.ls.getJSONObj('theme-themeInfo');
    if (themeInfo) {
      this.onLoadTheme(themeInfo);
      this.loading = false;
    }

    LOGIN_API.getMyTheme().then((result) => {
      const { data = {} } = result;
      let _themeInfo = data;
      const { themeType, themeData = {} } = _themeInfo;
      if (themeType == 'ecology9') {
        const { styleConfig = '{}' } = themeData;
        _themeInfo = { ...data, themeData: { ...themeData, styleConfig: JSON.parse(styleConfig.replace(/&quot;/g, '\"')) } };
      }

      if (!isEqual(_themeInfo, themeInfo)) {
        this.onLoadTheme(_themeInfo);
      }

      this.loading = false;
    });
  }

  @action
  onLoadTheme(themeInfo) {
    this.themeInfo = themeInfo;

    const { themeTitle = '', themeType, themeLayoutType, themeFontSize = '12', themeData = {} } = themeInfo;
    const { url = '' } = themeData;

    if (url) {
      WeaTools.ls.set('theme-themeInfo', null);
      window.location.href = url;
    } else {
      WeaTools.ls.set('theme-themeInfo', themeInfo);

      document.title = themeTitle;
      setFontSize(themeFontSize);
      if (themeType == 'ecology9') {
        if (themeLayoutType == '5') {
          setE9ThemeStyle(null);
        } else {
          const { styleConfig = {} } = themeData;
          setE9ThemeStyle(styleConfig);
        }
      }
    }
  }

  @action
  getAccount() {
    LOGIN_API.getAccount().then((result) => {
      const { data } = result;
      this.account = data;
      // WeaTools.ls.set('theme-account', data);
    });
  }

  @action
  getMenus() {
    // 先从本地浏览器缓存获取当前用户菜单，若缓存中没有，则从后台重新加载
    let menus = WeaTools.ls.getJSONObj('odoc_exchange_menus');
    if (menus) {
      this.menus = menus;
    } else {
      LOGIN_API.getMenus().then((result) => {
        const odoc_exchange_menus = result.menu;
        WeaTools.ls.set('odoc_exchange_menus', odoc_exchange_menus);
        this.menus = menus;
      });
    }
  }

  @action
  onAccountVisibleChange(visable) {
    this.accountVisible = visable;
    if (!visable) {
      this.fontSettingVisible = visable;
    }
  }

  @action
  onFontSettingVisibleChange(visable) {
    this.fontSettingVisible = visable;
  }

  @action
  onFontSizeChange(fontSize) {
    setFontSize(fontSize);
    LOGIN_API.setMyFontSize({ fontSize }).then(() => {
      const themeInfo = { ...this.themeInfo, themeFontSize: fontSize };
      this.themeInfo = themeInfo;
      WeaTools.ls.set('theme-themeInfo', themeInfo);
    });
  }

  logout() {
    Modal.confirm({
      title: getLabel('131329', '信息确认'),
      content: '确定要退出系统吗？',
      okText: getLabel('163', '是'),
      cancelText: getLabel('30587', '否'),
      onOk() {
        LOGIN_API.logout().then(() => hashHistory.push('/'));
      },
      onCancel() {
      },
    });
  }
}
